﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Chisel.Core;
using Chisel.Core.Connections;

namespace Chisel.Demo
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();

            Loaded += new RoutedEventHandler(OnLoaded);
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
			var entity1 = new Entity();
			AddEntityControls(entity1);
			new ResizeWrapper(entity1);
			entity1.Position = new Point(100, 100);
			_viewer.AddActor(entity1);

			var entity2 = new Entity();
			AddEntityControls(entity2);
			new ResizeWrapper(entity2);
			entity2.Position = new Point(400, 400);
			_viewer.AddActor(entity2);

			var entity3 = new Entity();
			AddEntityControls(entity3);
			new ResizeWrapper(entity3);
			entity3.Position = new Point(400, 100);
			_viewer.AddActor(entity3);

            entity1.AddChild(CreateItem("item 1"));
            entity1.AddChild(CreateItem("item 2"));
			var group1 = new ItemGroup("Group 1");
			group1.AddItem(CreateItem("Subitem 1"));
			group1.AddItem(CreateItem("Subitem 2"));
			entity1.AddChild(group1);
			var group2 = new ItemGroup("Group 2");
			group2.AddItem(CreateItem("Subitem 3"));
			group2.AddItem(CreateItem("Subitem 4"));
			entity1.AddChild(group2);

            entity2.AddChild(CreateItem("item 3"));
            entity2.AddChild(CreateItem("item 4"));
			entity2.AddChild(CreateItem("item 5"));
			var group3 = new ItemGroup("Group 3");
			group3.AddItem(CreateItem("Subitem 5"));
			group3.AddItem(CreateItem("Subitem 6"));
			entity2.AddChild(group3);

			var connection1 = new Connection() {
				Start = entity1,
				Stop = entity2,
				StartDecorator = new IdentityChickenLegDecorator(),
				StopDecorator = new ChickenLegDecorator()
			};
			AddConnectionControls(connection1);
			_viewer.AddActor(connection1);
        }

		/// <summary>
		/// Generates controls for the entities.
		/// </summary>
		private ControlsWrapper AddEntityControls(Entity entity)
		{
			var controls = new ControlsWrapper(entity) {
				Location = ControlLocation.North
			};
			var connectionButton = new Button() {
				Content = "Add Connection"
			};
			ToolTipService.SetToolTip(connectionButton, "Adds a connection to this entity");
			connectionButton.Click += delegate(object sender, RoutedEventArgs e)
			{
				OnAddConnection(entity);
			};
			controls.AddControl(connectionButton);

			var button2 = new Button() {
				Content = "World"
			};
			ToolTipService.SetToolTip(button2, "World button");
			controls.AddControl(button2);

			return controls;
		}

		/// <summary>
		/// Generates controls for the connections.
		/// </summary>
		private ControlsWrapper AddConnectionControls(Actor actor)
		{
			var controls = new ControlsWrapper(actor) {
				Location = ControlLocation.Center
			};
			var button1 = new Button() {
				Content = "Hello"
			};
			controls.AddControl(button1);
			var button2 = new Button() {
				Content = "World"
			};
			controls.AddControl(button2);

			return controls;
		}

		/// <summary>
		/// Creates an item and attached controls to it.
		/// </summary>
		private Item CreateItem(string name)
		{
			var item = new Item(name);
			var controls = new ControlsWrapper(item) {
				Location = ControlLocation.West
			};
			var deleteButton = new Button() {
				Content = new TextBlock() { Text = "Delete" }
			};
			controls.AddControl(deleteButton);
			return item;
		}



		/// <summary>
		/// Adds a connection to an entity.
		/// </summary>
		private void OnAddConnection(Entity entity)
		{
			var connection = new Connection() {
				Start = entity,
				StartDecorator = new IdentityChickenLegDecorator(),
				StopDecorator = new ChickenLegDecorator()
			};
			AddConnectionControls(connection);
			_viewer.AddActor(connection);
			connection.CaptureCursor();

		}

    }
}
